﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace alwitTest.FileSystem
{
    /// <summary>
    /// 文件操作类
    /// </summary>
    internal class FileHelper
    {
        private object m_synRoot = new object();
        /// <summary>
        /// 要读取的文件完全限定名
        /// </summary>
        /// <param name="path"></param>
        public FileHelper(string fileName)
        {
            string appPath = "";

            appPath = System.AppDomain.CurrentDomain.BaseDirectory;
            this.FilePath = appPath + fileName;

        }

        /// <summary>
        /// 属性 文件路径
        /// </summary>
        string filePath;
        public string FilePath
        {
            get { return filePath; }
            set { filePath = value; }
        }

        /// <summary>
        /// 得到文件的完全名字 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string FullFileName(string path)
        {
            return Path.GetFileName(path);//path不存在就返回path
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="context"></param>
        public void WriteFile(string context, string clsName, string methodName)
        {
            FileStream fileStream = null;
            StreamWriter sw = null;
            TextWriter tw = null;

            lock (m_synRoot)
            {
                try
                {
                    if (!File.Exists(this.FilePath)) /*判断文件是否存在，存在true不存在false*/
                    {
                        fileStream = new FileStream(this.FilePath, FileMode.Create, FileAccess.Write);
                    }
                    else
                    {
                        fileStream = new FileStream(this.FilePath, FileMode.Append, FileAccess.Write);
                    }

                    using (sw = new StreamWriter(fileStream))
                    {
                        tw = TextWriter.Synchronized(sw); //不明白这两个的区别，但是效果是一样地,看到网上有人这样写，因为安全？！谁了解讲讲啊

                        tw.Write("*** Error Encountered " + DateTime.Now.ToString() + " ***");
                        tw.WriteLine();
                        tw.Write("ClassName: " + clsName);
                        tw.WriteLine();
                        tw.Write("MethodName: " + methodName);
                        tw.WriteLine();
                        tw.Write("ErrorDescription: " + context);
                        tw.WriteLine();
                        tw.WriteLine();
                    }
                }
                catch
                {
                }
                finally
                {
                    if (tw != null)
                    {
                        tw.Close();
                    }
                    if (sw != null)
                    {
                        sw.Close();
                    }
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <returns></returns>
        public string ReadFile()
        {
            lock (m_synRoot)
            {
                using (StreamReader sr = File.OpenText(this.FilePath))
                {
                    //return sr.ReadToEnd();     //方案一从头一口气读到尾

                    StringBuilder sb = new StringBuilder(100); //方案二一小口气读一行，劳逸结合
                    string str = string.Empty;
                    if ((str = sr.ReadLine()) != null)
                    {
                        sb.Append(str);
                        sb.Append(@"\r\n");
                    }
                    return sb.ToString();
                }
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <returns></returns>
        public bool DeleteFile()
        {
            lock (m_synRoot)
            {
                try
                {
                    if (File.Exists(this.FilePath))
                    {
                        File.Delete(this.FilePath);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 复制文件,将我们的文件复制到一个新文件,新文件如果已经存在那么就挂了
        /// </summary>
        /// <param name="path"></param>
        public bool CopyFile(string path)
        {
            lock (m_synRoot)
            {
                try
                {
                    if (!File.Exists(path))
                    {
                        File.Copy(this.FilePath/*源文件*/ , path/*目标文件*/);
                        return true;//复制成功了
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }

    }

    internal class FileHelperProxy
    {
        private static FileHelper m_fileHelper;

        public static void InitializeFileHelper()
        {
            m_fileHelper = new FileHelper("CHLErrorData.txt");
        }

        public static FileHelper FileProcessHelper
        { get { return m_fileHelper; } }

    }

}
